require "Common/define"
require "Logic/Config"
require "Net/send_Msg"
require "proto/rune/message/ReqInlayMessage"
require "proto/rune/message/ReqUnInlayMessage"
require "proto/rune/message/ReqUpRuneLvMessage"
require "proto/rune/message/ReqRuneChangeMessage"
require "proto/rune/message/ReqLockPropertyMessage"
require "proto/rune/message/ReqRefineRuneMessage"

runeHandler = runeHandler or {};

function runeHandler.receivePartnerRunes(msg)
    if msg and msg.state == 0 then
        logicMgr.RuneManager.SaveHeroRuneData(msg.partnerRunes)
    end
end

function runeHandler.reqInlayRune(partnerId, itemId, index)
    local req = ReqInlayMessage.New()
    req.partnerId = partnerId
    req.itemId = itemId
    req.runeIndex = index
    sendReqMessage(req)
end

function runeHandler.resInlayRune(msg)
    if msg and msg.state == 0 then
        local runeInfo = logicMgr.RuneManager.FormationRuneInfo(msg.item, msg.partnerId)
        local dic = {}
        dic.curRuneId = runeInfo.itemModelId
        dic.partnerId = runeInfo.partnerId
        dic.runeLv = runeInfo.level
        local oldRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex)
        if oldRuneInfo then
            dic.oldRuneId = oldRuneInfo.itemModelId
        end
        TAManager.TrackByDictionary("RuneInlay",dic)
        logicMgr.RuneManager.SetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex, runeInfo)
        logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,true)
        FireEvent(Config.EventType.FreshHeroRune)
        FireEvent(Config.EventType.ReFreshHeroPower)
    end
end

function runeHandler.reqChangeRune(partnerId, runeIndex, otherPartnerId, otherRuneIndex)
    local req = ReqRuneChangeMessage.New()
    req.partnerId = partnerId
    req.runeIndex = runeIndex
    req.otherPartnerId = otherPartnerId
    req.otherRuneIndex = otherRuneIndex
    sendReqMessage(req)
end

function runeHandler.resExchangeRune(msg)
    if msg and msg.state == 0 then
        logicMgr.RuneManager.ExchageHeroRuneData(msg.partnerId, msg.runeIndex, msg.otherPartnerId, msg.otherRuneIndex)
        logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,true)
        logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,false)
        FireEvent(Config.EventType.FreshHeroRuneExchange)
        FireEvent(Config.EventType.ReFreshHeroPower)
    end
end

function runeHandler.reqUnInlayRune(partnerId, itemId, index)
    local req = ReqUnInlayMessage.New()
    req.partnerId = partnerId
    req.itemId = itemId
    req.runeIndex = index
    sendReqMessage(req)
end

function runeHandler.resUnInlayRune(msg)
    if msg and msg.state == 0 then
        local oldRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex)
        if oldRuneInfo then
            local dic = {}
            dic.runeId = oldRuneInfo.itemModelId
            dic.partnerId = oldRuneInfo.partnerId
            dic.runeLv = oldRuneInfo.level
            TAManager.TrackByDictionary("RuneUnInlay",dic)
        end
        logicMgr.RuneManager.SetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex, nil)
        logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,true)
        FireEvent(Config.EventType.FreshHeroRune)
        FireEvent(Config.EventType.ReFreshHeroPower)
    end
end

function runeHandler.reqUpRuneLv(partnerId, itemId, index,costItems)
    local req = ReqUpRuneLvMessage.New()
    req.partnerId = partnerId
    req.itemId = itemId
    req.runeIndex = index
    req.items = costItems
    local byte_size = 0
    local costList  ={}
    for _, v in pairs(costItems) do
        local item = CostItemInfo.New()
        item.itemModelId = tonumber(v.modelid)
        item.itemId = v.itemid
        item.itemNum = v.num or 1
        table.insert(costList, item)
        byte_size = byte_size+20
    end
    req.items = costList
    byte_size = byte_size + 100
    sendSmartReqMessage(req, nil, nil, byte_size)
end

function runeHandler.resUpRuneLv(msg)
    if msg and msg.state == 0 then
        if msg.partnerId and msg.runeIndex then
            local runeInfo = logicMgr.RuneManager.FormationRuneInfo(msg.item, msg.partnerId)
            local oldRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex)
            if runeInfo then
                if oldRuneInfo then
                    local dic = {}
                    dic.runeId = runeInfo.itemModelId
                    dic.runeOldLv = oldRuneInfo.level
                    dic.runeCurLv = runeInfo.level
                    TAManager.TrackByDictionary("RuneLevelUp",dic)
                end
                logicMgr.RuneManager.SetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex, runeInfo)
                logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,true, nil, nil, nil, true)
                FireEvent(Config.EventType.FreshHeroRuneLevelUp)
                FireEvent(Config.EventType.ReFreshHeroPower)
            end
        end
    end
end

function runeHandler.reqRuneLock(partnerId, itemId, index, propertyIndex)
    local req = ReqLockPropertyMessage.New()
    req.partnerId = partnerId
    req.itemId = itemId
    req.runeIndex = index
    req.propertyIndex = propertyIndex
    sendReqMessage(req)
end

function runeHandler.resRuneLock(msg)
    if msg and msg.state == 0 then
        if msg.partnerId and msg.runeIndex then
            local runeInfo = logicMgr.RuneManager.FormationRuneInfo(msg.item, msg.partnerId)
            logicMgr.RuneManager.SetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex, runeInfo)
        end
        FireEvent(Config.EventType.FreshHeroRuneLock)
    end
end

function runeHandler.reqBaptismRune(partnerId, itemId, runeIndex,costItemId)
    local req = ReqRefineRuneMessage.New()
    req.partnerId = partnerId
    req.itemId = itemId
    req.runeIndex = runeIndex
    req.costItemId = costItemId
    sendReqMessage(req)
    --local byte_size = 0
    --local costList  ={}
    --for _, v in pairs(costItems) do
    --    local item = CostItemInfo.New()
    --    item.itemModelId = tonumber(v.modelid)
    --    item.itemId = v.itemid
    --    item.itemNum = v.num or 1
    --    table.insert(costList, item)
    --    byte_size = byte_size+20
    --end
    --req.costItem = costList
    --byte_size = byte_size + 100
    --sendSmartReqMessage(req, nil, nil, byte_size)
end

function runeHandler.resBaptismRune(msg)
    if msg and msg.state == 0 then
        if msg.partnerId and msg.runeIndex then
            local runeInfo = logicMgr.RuneManager.FormationRuneInfo(msg.item, msg.partnerId)
            local oldRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex)
            local dic = {}
            dic.runeId = runeInfo.itemModelId
            dic.oldBaptism = oldRuneInfo.baptismAttrs
            dic.curBaptism = runeInfo.baptismAttr or {}
            dic.lockNum = #runeInfo.lockList or {}
            TAManager.TrackByDictionary("RuneBaptism",dic)

            logicMgr.RuneManager.SetHeroRuneSrvInfo(msg.partnerId, msg.runeIndex, runeInfo)
            logicMgr.HeroManager.updateHeroBaseAttr(msg.partnerId,true, nil, nil, nil, true)
            FireEvent(Config.EventType.ReFreshHeroPower)
        end
        FireEvent(Config.EventType.FreshHeroRuneBaptism)
    end
end

